﻿using UnityEngine;
using System.Collections;

public class CreateGame : MonoBehaviour {

	public GameObject[] levels;
	public float NewLevelTimer = 20.0f;
	float timer;
	GameObject currentLevel;
	// Use this for initialization
	void Start () {
		Camera.main.aspect = 16f/9f;
		timer = 0.0f;
	}
	GameObject NewLevel()
	{

		int randLevel = Random.Range(0,levels.Length);
		GameObject newLevel = (GameObject)Instantiate(levels[randLevel],this.transform.position,levels[randLevel].transform.rotation);
		newLevel.transform.parent = this.transform;
		Debug.Log("NewLevel for " + this.name + " : " + newLevel.ToString());
		return newLevel;
	}
	// Update is called once per frame
	void Update () {
		timer -= Time.deltaTime;

		if(timer < 0.0f)
		{
			timer = NewLevelTimer;

			if(currentLevel != null)
			{
				GameObject tempLevel = currentLevel;
				Destroy(tempLevel);
			}
			 currentLevel = NewLevel();

		}
	}
}
